package com.yanqu.road.server.manager.activity.xingyou.inter.grid;

import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamActionLogData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.entity.log.cross.xingyou.LogCrossXingYouBaoHua;
import com.yanqu.road.entity.log.cross.xingyou.LogCrossXingYouSlive;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.xingyou.ConfirmHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouActivity;
import com.yanqu.road.server.manager.activity.xingyou.DiceHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.GridHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.IXingYouGridHandler;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 宝华街
 */
public class BaoHuaGridHandler implements IXingYouGridHandler {

    @Override
    public void handle(XingYouShanShuiGridInfo gridInfo, int pos, int gridId, int endPos, boolean fly, DiceHandleResult result) {
        //是否停留
        boolean stop = pos == endPos;
        if(!stop){
            return;
        }

        CrossXingYouActivity activity = result.getActivity();

        GridHandleResult gridHandleResult = new GridHandleResult();
        ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, false, result.getUserData().isDrift());
        gridHandleResult.setGridParam(gridParam);
        //固定奖励
        Property fixedReward = activity.getGridInfoFixedReward(result.getUserData(), gridInfo, true);

        XingYouShanShuiProto.ShanShuiEventInfo.Builder eventBuilder = activity.getEventInfoBuilder(pos, gridId, fixedReward, false);
        //固定奖励，单独放，外部不要再加了
        eventBuilder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));

        gridHandleResult.setBuilder(eventBuilder);

        result.getGridHandleResultList().add(gridHandleResult);

        CrossXingYouShanShuiUserData userData = result.getUserData();
        //是否有捕快
        activity.tryCatch(pos, userData, result, gridHandleResult);
    }

    @Override
    public void confirm(XingYouShanShuiGridInfo gridInfo, CrossXingYouShanShuiUserData userData, ConfirmHandleResult handleResult, long select, boolean isDrift) {
        int index = (int)select - 1;
        XingYouConfig config = handleResult.getActivity().getConfig();
        List<String> stringList = StringUtils.stringToStringList(config.getSHANSHUI_SHOP_CONFIG(), ";");
        String s = stringList.get(index);
        List<String> itemList = StringUtils.stringToStringList(s, "\\|");
        String reward = itemList.get(0);
        int cost = Integer.parseInt(itemList.get(1));
        CrossXingYouShanShuiTeamData teamData = handleResult.getActivity().getTeamData(userData.getTeamId());
        synchronized (handleResult.getActivity().getTeamLocker(userData.getTeamId())){
            ShanShuiGridParam gridParam = userData.getGridParam();
            if(gridParam.isDrift()){
                gridParam = userData.getDriftGridParam();
            }
            if(gridParam.getBuy() > 0){
                handleResult.setRet(GameErrorCode.E_XING_YOU_SHAN_SHUI_GRID_HANDLED);
                return;
            }
            if(teamData.getSilverCoin() < cost){
                handleResult.setRet(GameErrorCode.E_XING_YOU_SHAN_SHUI_SILVER_NO_ENOUGH);
                return;
            }
            gridParam.setHandled(true);
            teamData.setSilverCoin(teamData.getSilverCoin() - cost);
            handleResult.getReward().addProperty(PropertyHelper.parseStringToProperty(reward));

            //日志
            LogCrossXingYouSlive logCrossXingYouSlive = new LogCrossXingYouSlive(teamData.getTeamId(), teamData.getActivityId(), userData.getUserId(), 2, cost, teamData.getSilverCoin(), 3);
            logCrossXingYouSlive.setGroupId(userData.getGroupId());
            CrossAutoLogMgr.add(logCrossXingYouSlive);

            //队伍日志:宝华御街
            CrossXingYouShanShuiTeamActionLogData actionLogData = handleResult.getActivity().createActionLogData(userData, XingYouShanShuiProto.ShanShuiLogTypeEnum.SHAN_SHUI_LOG_OCEANIC_TRADE_CENTER,
                    reward, Arrays.asList(String.valueOf(userData.getRoleId()), config.getSHANSHUI_SILVER_COIN_ID() + "=" + cost));
            handleResult.getActivity().addCrossXingYouShanShuiTeamActionLogData(actionLogData);

            LogCrossXingYouBaoHua logData = new LogCrossXingYouBaoHua(userData.getActivityId(), userData.getTeamId(), userData.getUserId(), reward, cost);
            handleResult.getAutoLogList().add(logData);
        }
    }
}
